package com.kznnc.data_structure.structure.stackStructure;

/**
 * @Description 数组栈  已数组作为栈的存储结构
 * @Author houchenkai
 * @Created Date: 2021/4/6 19:27
 * @ClassName
 */
public class ArrayStack {

    private Integer[] data = null;

    private int size = 0;

    public ArrayStack(int capacity) {
        this.data = new Integer [capacity];
    }

    public void push (Integer num){
        if (size >= data.length){
            throw new RuntimeException("栈溢出");
        }else {
            data[size++]=num;
        }
    }

    public Integer take (){
        if (size == 0 ){
            throw new RuntimeException("栈无数据");
        }else {
            Integer num = data[--size];
            data[size] = null;
            return num;
        }
    }

    public void  getData(){
        for (int i = 0; i < size; i++) {
            System.out.println("第"+i+"个元素:=="+data[i]);
        }
    }

    public static void main(String[] args) {
        ArrayStack stack = new ArrayStack(3);
        for (int i = 0; i < 3; i++) {
            stack.push(i);
        }
        System.out.println(stack.take());
        stack.getData();
    }
}
